package com.example.forum_system.services.impl;

import com.example.forum_system.common.AppResult;
import com.example.forum_system.common.ResultCode;
import com.example.forum_system.dao.ArticleReplyMapper;
import com.example.forum_system.exception.ApplicationException;
import com.example.forum_system.model.Article;
import com.example.forum_system.model.ArticleReply;
import com.example.forum_system.services.IArticleReplyService;
import com.example.forum_system.services.IArticleService;
import com.example.forum_system.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: 啵啵虎
 * @Date: 2024-05-25  18:30
 */
@Service
@Slf4j
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;

    @Resource
    private IArticleService articleService;

    @Override
    public void create(ArticleReply articleReply) {
        //非空校验
        if (articleReply==null || articleReply.getPostUserId()==null || articleReply.getPostUserId()<=0
            || articleReply.getArticleId()==null || articleReply.getArticleId()<=0
            || StringUtils.isEmpty(articleReply.getContent())){
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //获取帖子详情
        Article article=articleService.selectDetailById(articleReply.getArticleId());
        if (article==null || article.getDeleteState()==1){
            //帖子不存在
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if (article.getState()==1){
            //帖子封帖
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_STATE));
        }
        //设置默认值
        articleReply.setLikeCount(0);  //点赞数
        articleReply.setState((byte) 0);  //状态
        articleReply.setDeleteState((byte) 0);  //删除状态
        Date date=new Date();
        articleReply.setCreateTime(date);  //创建时间
        articleReply.setUpdateTime(date);  //更新时间
        //调用DAO
        int row=articleReplyMapper.insertSelective(articleReply);
        if (row!=1){
            //打印日志
            log.info(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //更新帖子的回复数
        Article updateArticle=new Article();
        updateArticle.setId(article.getId());  //帖子Id
        updateArticle.setReplyCount(article.getReplyCount()+1);  //回复数
        updateArticle.setUpdateTime(new Date());  //更新时间
        //调用帖子的更新方法
        articleService.updateById(updateArticle);
        //打印日志
        log.info("回复成功，article id="+article.getId());
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        //非空校验
        if (articleId==null || articleId<=0){
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验主帖是否存在
        Article article=articleService.selectDetailById(articleId);
        if (article==null || article.getDeleteState()==1){
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //调用DAO
        List<ArticleReply> articleReplies=articleReplyMapper.selectByArticleId(articleId);
        //返回结果集
        return articleReplies;
    }
}
